package com.hh.baseframework.util.io;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

public class FileUtil {
	
	public static void isExist(List<String> paths) {
		for (String string : paths) {
			isExist(string);
		}
	}
	public static void isExist(String path) {
		File file = new File(path);
		if (!file.exists()) {
			file.mkdir();
		}
	}

	public static void getFilePathListByFileRoot(String root,
			List<String> filePathList) {
		File file = new File(root);
		File[] subFile = file.listFiles();
		for (int i = 0; i < subFile.length; i++) {
			if (subFile[i].isDirectory()) {
				getFilePathListByFileRoot(subFile[i].getAbsolutePath(),
						filePathList);
			} else {
				String filename = subFile[i].getName();
				if (filename == null ? false : filename.length() < 3 ? false
						: true) {
					if (".js".equals(filename.substring(filename.length() - 3))) {
						filePathList.add(root.replace("\\", "/") + "/"
								+ filename);
					}
				}
			}
		}
	}

	public static void getRelativityFilePathListByFileRoot(String filepath,
			List<String> filePathList, String pathString) {
		File file = new File(filepath);
		File[] subFile = file.listFiles();
		for (int i = 0; i < subFile.length; i++) {
			if (subFile[i].isDirectory()) {
				getFilePathListByFileRoot(subFile[i].getAbsolutePath(),
						filePathList);
			} else {
				String filename = subFile[i].getName();
				if (filename == null ? false : true) {
					if (filename.indexOf("(") < 0 && filename.indexOf(")") < 0) {
						filePathList.add(pathString + "/" + filename);
					}
				}
			}
		}
	}

	public static byte[] stream2Byte(InputStream inputStream) {
		BufferedInputStream in = null;
		ByteArrayOutputStream out = null;
		try {
			in = new BufferedInputStream(inputStream);
			out = new ByteArrayOutputStream(1024);

			byte[] temp = new byte[1024];
			int size = 0;
			while ((size = in.read(temp)) != -1) {
				out.write(temp, 0, size);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				inputStream.close();
				in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		byte[] content = out.toByteArray();
		return content;
	}

	public void saveFileFromInputStream(InputStream stream, String path,
			String filename) {
		FileOutputStream fs = null;
		try {

			fs = new FileOutputStream(path + "/" + filename);
			byte[] buffer = new byte[1024 * 1024];
			int bytesum = 0;
			int byteread = 0;
			while ((byteread = stream.read(buffer)) != -1) {
				bytesum += byteread;
				fs.write(buffer, 0, byteread);
				fs.flush();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (fs != null) {
					fs.close();
				}
				stream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static void main(String[] args) throws IOException {
		File inFile = new File("java.txt");
		File outputFile = new File("result.txt");
		String fileContextStr = readFile(inFile);
		writeToResultFile(outputFile, fileContextStr);
	}

	public static void writeToResultFile(File file, String result)
			throws IOException {
		FileOutputStream fo = new FileOutputStream(file);
		try {
			fo.write(result.getBytes("utf-8"));
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			fo.close();
		}

	}

	public static String readFile(File file) throws FileNotFoundException,
			IOException {
		StringBuffer sb = new StringBuffer();
		InputStream in = new FileInputStream(file);
		InputStreamReader inputStreamReader = new InputStreamReader(in,"utf-8");
		BufferedReader bf = new BufferedReader(inputStreamReader);
		try {
			String context = null;
			do {
				context = bf.readLine();
				if (context != null) {
					sb.append(context);
				}
			} while (context != null);

			return sb.toString();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			bf.close();
			in.close();
			inputStreamReader.close();
		}
		return "";
	}

}
